package com.ait.cms.service.impl;

import com.ait.cms.entity.UcenterMember;
import com.ait.cms.entity.vo.RegisterVo;
import com.ait.cms.mapper.UcenterMemberMapper;
import com.ait.cms.service.UcenterMemberService;
import com.ait.commonutils.Jwt.JwtUtils;
import com.ait.commonutils.MD5;
import com.ait.commonutils.exceptionHandler.GuilException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-07-01
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    /*
    * 查看用户有没有登录
    *
    *
    * */

    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        //账号密码不能为空
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password))throw new GuilException(404,"登录异常");
        //获取账号
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember ucenterMember = this.getOne(wrapper);

        //判断账号是否存在和密码是否正确，该用户是否被禁用
        if (ucenterMember==null)throw new GuilException(404,"用户不存在");
        if(!ucenterMember.getPassword().equals(MD5.encrypt(password)))throw new GuilException(404,"密码错误");
        if (ucenterMember.getIsDisabled())throw new GuilException(404,"用户被禁用");

        //登陆成功

        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(),ucenterMember.getNickname());
        return jwtToken;
    }


    @Override
    public boolean register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String nickname = registerVo.getNickname();

        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)||StringUtils.isEmpty(nickname))throw new GuilException(404,"密码不能为空");

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();

        wrapper.eq("mobile",mobile);

        if (this.count(wrapper)>0) throw new GuilException(404,"该用户已经被注册");

        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setNickname(nickname);
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAge(0);
        ucenterMember.setSex(2);

        ucenterMember.setAvatar("https://gui-lizhuang.oss-cn-zhangjiakou.aliyuncs.com/2021/06/25/f41f16c1.jpg");
        boolean save = this.save(ucenterMember);
        return save;
    }

    public boolean modify(RegisterVo registerVo){
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        if (StringUtils.isEmpty(password)||StringUtils.isEmpty(mobile))throw new GuilException(404,"密码不能为空");
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setPassword(MD5.encrypt(password));
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        return this.update(ucenterMember,wrapper);
    }
}
